<?php

namespace App\Admin\Controllers\User;

use App\Models\User\SystemUser;
use App\Models\User\UserWalletrecord;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class WalletController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '钱包';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserWalletrecord());
        $uuid = request('uuid','');

        $grid->model()->orderBy('created_at','desc');
//        ->when($uuid, function ($query,$value){
//            $user = SystemUser::where('uuid', $value)->firstOrFail();
//            $query->where('user_id', $user->id);
//        });
        $grid->filter(function ($filter) {
//            // 去掉默认的id过滤器
//            $filter->disableIdFilter();
////            $filter->equal('user_id')

//            dd($data);
            /*$data = SystemUser::query()->select(['uuid', 'nickname as text'])->limit(100)->get()->pluck('text','uuid')->toArray();
            $filter->select('SystemUser.id','用户2')->options($data);
            */
            $filter->equal('SystemUser.uuid','用户')->select(function(){
//                $data = SystemUser::query()->select(['uuid', 'nickname as text'])->limit(100)->get()->pluck('text','uuid')->toArray();
                return [1 => 'dongd'];
            })->ajax("/admin/api/users");
////            adminLoadSelect2LangJs();
        });

        $grid->column('id', __('UserWalletrecord.id'));
        $grid->column('SystemUser.no', __('SystemUser.no'));
        $grid->column('content', __('UserWalletrecord.content'));
        $grid->column('money', __('UserWalletrecord.money'));
        $grid->column('direction', __('UserWalletrecord.direction'))->using(UserWalletrecord::DIRECTION_ARR);
        $grid->column('remaining_amount', __('UserWalletrecord.remaining_amount'));
        $grid->column('before_amount', __('UserWalletrecord.before_amount'));
//        $grid->column('type', __('UserWalletrecord.type'))->using(UserWalletrecord::TYPE_ARR);
//        $grid->column('type_id', __('UserWalletrecord.type_id'));
        $grid->column('created_at', __('UserWalletrecord.created_at'));
        $grid->column('updated_at', __('UserWalletrecord.updated_at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserWalletrecord::findOrFail($id));

        $show->field('id', __('UserWalletrecord.id'));
        $show->field('user_id', __('UserWalletrecord.user_id'));
        $show->field('content', __('UserWalletrecord.content'));
        $show->field('money', __('UserWalletrecord.money'));
        $show->field('direction', __('UserWalletrecord.direction'));
        $show->field('type', __('UserWalletrecord.type'));
        $show->field('type_id', __('UserWalletrecord.type_id'));
        $show->field('remaining_amount', __('UserWalletrecord.remaining_amount'));
        $show->field('before_amount', __('UserWalletrecord.before_amount'));
        $show->field('created_at', __('UserWalletrecord.created_at'));
        $show->field('updated_at', __('UserWalletrecord.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserWalletrecord());

        $form->select('user_id', __('UserWalletrecord.user_id'))->options(function(){
//                $data = SystemUser::query()->select(['uuid', 'nickname as text'])->limit(100)->get()->pluck('text','uuid')->toArray();
            return [1 => 'dongd'];
        })->ajax("/admin/api/users");
        $form->text('content', __('UserWalletrecord.content'));
        $form->decimal('money', __('UserWalletrecord.money'));
        $form->number('direction', __('UserWalletrecord.direction'))->default(1);
        $form->text('type', __('UserWalletrecord.type'))->default('default');
        $form->number('type_id', __('UserWalletrecord.type_id'));
        $form->decimal('remaining_amount', __('UserWalletrecord.remaining_amount'))->default(0.00);
        $form->decimal('before_amount', __('UserWalletrecord.before_amount'))->default(0.00);

        return $form;
    }
}
